<?php
if($enviar)
   {
     if(!is_numeric($rut) || !is_numeric($digito))
       {
        echo "<script>alert('Debe ingresar solo números')</scritp>";
        if($rut=="" || $digito=="" || $espectaculo=="Seleccione" || $local=="Seleccione" || $cantEntrada=="Seleccione" || $entrada=="Seleccione")
           {
           echo "<script>alert('Faltan datos')</scritp>";
           }
       }
        else
       {
        include("../conexion.php");
        $consultaEnt = mysql_query("Select EntradasDisponibles from espectaculo where descripcion = '$espectaculo'");
        while ($row = mysql_fetch_row($consultaEnt))
            {
		        $entradas=$row[0];
            }
             if($entradas<$cantEntrada || $entradas==0)
               {
                 echo "<script>alert('No hay entradas suficientes')</script>";
               } 
                else
               {
                $idCliente= $rut.'-'.$digito;
                $consultaId = mysql_query("Select IdEsp from espectaculo where descripcion = '$espectaculo'");
                while ($row = mysql_fetch_row($consultaId))
                      {
		               $id=$row[0];
                      }
		        $cont=0;
		        while($cont<$cantEntrada)
		            {
		               $sql= "insert into ventaDeEntradas.entrada(descripcion, IdEsp, IdCliente, FechaCompra) 
                       values('$entrada','$id','$idCliente',CURDATE());"; 
                       mysql_query($sql);
					   $cont++;
		            }
		        $sql= "Update ventaDeEntradas.cliente set cantEntradas = '$cantEntrada' where Rut = '$idCliente'"; 
                mysql_query($sql);
		        $sql= "Update ventaDeEntradas.espectaculo set EntradasDisponibles = EntradasDisponibles - '$cantEntrada' where IdEsp = '$id'"; 
                mysql_query($sql);
		        echo "<script>alert('Entradas reservadas')</script>";
               }
       }
   }
?>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="">
    <meta name="author" content="">
    <title>Raccoon Sell</title>
    <link href="../css/bootstrap.css" rel="stylesheet">
    <link href="../css/small-business.css" rel="stylesheet">
    <link rel="stylesheet" type="text/css" href="../css/form.css">
  </head>
  <body>
    <nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
      <div class="container">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand logo-nav" href="../index.html"><img src="../images/logo.png"></a>
        </div>
        <div class="collapse navbar-collapse navbar-ex1-collapse">
              <ul class="nav navbar-nav navbar-right">
	<li style="width:100px; color:white;"><center>Usuario</center></li>
	<li style="width:100px;"><center><a href="inicioCliente.html">Principal</a></center></li>
	<li style="width:100px;"><center><a href="formBuscaEspectaculo.html">Buscar</a></center></li>
	<li style="width:100px;"><center><a href="formReserva.html">Reservar</a></center></li>
    <li style="width:100px;"><center><a href="../index.php">Salir</a></center></li>
    </ul>
        </div>
      </div>
    </nav>
    <form style="width:380px;"class='contacto' method="post" action="" name="f_contacto">
        <h1><center>Reserva de entradas</center></h1><br>
        <label>Rut:</label>
		<table>
		<td>
		<input type="text" name="rut" style="width:100px;">
		</td>
		<td>-</td>
		<td>
		<input type="text" name="digito"style="width:20px;">
		</td>
		</table><br>
		<label>Espectaculo:</label>
		
		<?
		include("../conexion.php");
        $sql= "Select descripcion from espectaculo"; 
        $result = mysql_query($sql);
		echo "<select size='1' name='espectaculo'>";
		while ($row = mysql_fetch_row($result))
        {
         echo "<option>";
         echo $row[0]."</option>";
        }
		?>
		</select><br><br>
		<label>Cantidad de entradas:</label>
		<select size="1" name="cantEntrada">
		<option>Seleccione</option>
		<option>1</option>
        <option>2</option>
		<option>3</option>
        <option>4</option>
		<option>5</option>
        <option>6</option>
		<option>7</option>
        <option>8</option>
		<option>9</option>
        <option>10</option>
		<option>11</option>
        <option>12</option>
		<option>13</option>
        <option>14</option>
		<option>15</option>
		</select><br><br>
		<label>Entrada:</label>
		<select size='1' name='entrada'>
		<option>Galeria</option>
		<option>Cancha</option>
		<option>VIP</option>
		</select><br>
		<br><br>
		<input type='submit' value='Enviar' name="enviar">
		<input type='reset' value='Borrar'>	
	</form>
    <script src="../js/jquery.js"></script>
    <script src="../js/bootstrap.js"></script>
  </body>
</html>